import lodash from 'lodash';

export class menuToRouter {
	constructor(menu) {
		return this.formatRouter(menu);
	}
	formatRouter(arr) {
		let menus = [];
		arr.forEach(item => {
			let obj = {};
			let routeUrl = item.url.split('?');
			obj['path'] = routeUrl[0];
			obj['name'] = this.getRouterName(routeUrl[0]);
			obj['query'] = this.getRouterQuery(routeUrl[1]);
			obj['isPublic'] = item.isPublic;
			obj['meta'] = {
				id: item.id,
				title: item.name,
				level: item.menuLevel,
				icon: item.icon,
				type: item.style
			}
			if (!!item.childList && item.childList.length > 0) {
				obj['children'] = this.formatRouter(item.childList);
			}
			menus.push(obj);
		})
		return menus;
	}
	getRouterName(data) {
		let nameText = '';
		let nameArr = data.split('/').filter(item => !!item);
		nameArr.forEach(item => {
			nameText += lodash.capitalize(item)
		})
		return nameText;
	}
	getRouterQuery(str) {
		let obj = {};
		let strArr = !str ? [] : str.split('&');
		strArr.forEach(item => {
			let arr = item.split('=');
			obj[arr[0]] = arr[1];
		})
		return obj;
	}
}